<#assign tableName="${table.comment?replace('信息','')?replace('表','')}"/>
<#assign entityCap="${entity?cap_first}"/>

import { Module } from 'vuex'
import { IRootState } from '../type'
import { I${entity}State } from './type'
import {
  pageSearchRequest,
  deleteByIdRequest,
  add${entity}Request,
  update${entity}ByIdRequest
} from '@/service/${controllerMappingHyphen}/${controllerMappingHyphen}'
import { IPageSearchForm } from '@/service/type'
import {
  I${entity}SearchQuery,
  I${entity}Data,
  Update${entity}Form
} from '@/service/${controllerMappingHyphen}/type'

const ${entityCap}Module: Module<I${entity}State, IRootState> = {
  namespaced: true,
  state() {
    return {
      total: 0,
      pageListData: []
    }
  },
  getters: {
    pageListData(state) {
      return state.pageListData
    },
    pageListCount(state) {
      return state.total
    }
  },
  mutations: {
    changeListData(state, ${entityCap}List: I${entity}Data[]) {
      state.pageListData = ${entityCap}List
    },
    changeListCount(state, total: number) {
      state.total = total
    }
  },
  actions: {
    //分页条件查询
    async pageListAction(
      { commit },
      payload: IPageSearchForm<I${entity}SearchQuery>
    ) {
      const pageResult = await pageSearchRequest(payload)
      const { total, rows } = pageResult.data

      commit(`changeListData`, rows)
      commit(`changeListCount`, total)
    },
    //根据ID删除${tableName}数据
    async deletePageDataAction({ dispatch }, payload: any) {
      const { id } = payload

      //删除数据
      await deleteByIdRequest(id)

      //重新请求最新的数据
      dispatch('pageListAction', {
        page: 1,
        size: 10
      })
    },
    //增加${tableName}信息
    async add${entity}Action({ dispatch }, payload: I${entity}Data) {
      return await add${entity}Request(payload)
    },
    //修改${tableName}信息
    async update${entity}Action({ dispatch }, payload: Update${entity}Form) {
      return await update${entity}ByIdRequest(payload)
    }
  }
}

export default ${entityCap}Module
